package ro.jproject.imc.model;

public class CnpVerification {
	


	public static int[] getCnp(String cnp){
		
		
		int[] cnpArray = new int[13];
		
		for (int i = 0; i < cnp.length(); i++) {
			cnpArray[i] = Integer.parseInt(String.valueOf(cnp.charAt(i)));
		}
		
		return cnpArray;
	
	}
	
	public boolean cnpVerif(String cnp) {
		if(cnp.length() == 13){
			int[] cnpArray = CnpVerification.getCnp(cnp);
			long validator = cnpArray[0]*2 + cnpArray[1] * 7 + cnpArray[2]*9 + cnpArray[3]*1 + cnpArray[4]*4 + cnpArray[5]*6 +
					         cnpArray[6]*3 + cnpArray[7]*5 + cnpArray[8]*8 + cnpArray[9]*2 + cnpArray[10]*7 + cnpArray[11]*9;
			
			long rest = validator % 11;
			
			if(((rest < 10) && (rest == cnpArray[12])) || ((rest == 10) && (cnpArray[12] == 1))) {
				System.out.println("CNP valid ");
				return true;
			} 
		}
		System.out.println("CNP invalid");
		return false;
	}	
	
}	
	

